import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class FFListen<T> {
  Animation? animation;
  AnimationController? controller;

  start({
    required TickerProvider vsync,
    required ValueNotifier<T> rotateNotifier,
    required T begin,
    required T end,
    bool? repeat,
    Function(T value)? changeFun,
    Duration? duration,
    Function()? finishFun,
  }) {

    controller = AnimationController(
        duration: duration ?? const Duration(milliseconds: 200), vsync: vsync);
    animation = Tween<T>(begin: begin, end: end).animate(controller!)
      ..addListener(
        () {

          rotateNotifier.value = animation!.value;
          if (changeFun != null) {
            changeFun(animation!.value);
          }

          if (controller?.isCompleted == true) {
            controller?.dispose();
            if (finishFun != null) {
              finishFun();
            }
          }
        },
      );

    if (repeat == true) {
      controller?.repeat();
    }  else {
      controller?.forward(from: 0);
    }
  }
}

class FFListenGroup<T> {
  FFListen<T> listen;
  ValueNotifier<T> valueListenable;

  FFListenGroup({
    required this.listen,
    required this.valueListenable,
  });
}

//final ValueNotifier<double> valueListenable = ValueNotifier(0);
ValueListenableBuilder<T> globalValueListen<T>({
  required ValueListenable<T> valueListenable,
  required Widget Function(BuildContext ctx, T tValue, Widget? child) builder,
}) {
  return ValueListenableBuilder<T>(
      valueListenable: valueListenable, builder: builder);
}
